package parsers;

import object.Course;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public class CourseParser {
	
	private Course course;
	
	public CourseParser(){
		
	}
	
	public CourseParser(Course course){
		this.course=course;
	}
	
	public void setCourse(Course course){
		this.course=course;
	}
	
	public Element parse(Document doc){
		if(doc==null||course==null){
			System.err.println("doc or course cannot be null!");
			return null;
		}
		String cid=course.getCid();
		String cname=course.getCname();
		String cinfo=course.getCinfo();
		int count=course.getCount();
		
		Element courseE=doc.createElement("课程");
		
		Element cidE=doc.createElement("课程编号");
		Text cidT=doc.createTextNode(cid);
		courseE.appendChild(cidE);
		cidE.appendChild(cidT);
		
		Element cnameE=doc.createElement("课程名称");
		Text cnameT=doc.createTextNode(cname);
		courseE.appendChild(cnameE);
		cnameE.appendChild(cnameT);
		
		if(cinfo!=null){
			Element cinfoE=doc.createElement("课程描述");
			Text cinfoT=doc.createTextNode(cinfo);
			courseE.appendChild(cinfoE);
			cinfoE.appendChild(cinfoT);
		}
		if(count!=0){
			Element countE=doc.createElement("课时");
			Text countT=doc.createTextNode(String.valueOf(count));
			courseE.appendChild(countE);
			courseE.appendChild(countT);
		}
		
		return courseE;
	}

}
